package ppjava12v1.chess.entities.games;

import ppjava12v1.chess.Exceptions.IncorrectPositionException;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.io.Serializable;

/**
 * Created by Reyst on 21.09.2014.
 * <p/>
 * Координаты фигуры на доске
 */
@Embeddable
public class Coordinate implements Serializable {

    @Column(name = "row")
    private int row;

    @Column(name = "col")
    private int col;

    public Coordinate(int row, int col) throws IncorrectPositionException {
        if (checkCol(col) && checkRow(row)) {
            this.row = row;
            this.col = col;
        } else
            throw new IncorrectPositionException(String.format("%s%d is incorrect position", col, row));
    }

    public Coordinate() {
        row = 1;
        col = 1;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) throws IncorrectPositionException {
        if (checkRow(row))
            this.row = row;
        else
            throw new IncorrectPositionException(String.valueOf(row) + " is incorrect row.");
    }

    private boolean checkRow(int row) {
        return (row > 0 && row < 9);
    }

    private boolean checkCol(int col) {
        return  (col > 0 && col < 9);
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) throws IncorrectPositionException {
        if (checkRow(col))
            this.col = col;
        else
            throw new IncorrectPositionException(col + " is incorrect column.");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Coordinate)) return false;

        Coordinate that = (Coordinate) o;

        if (row != that.row) return false;
        if (col != that.col) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = row;
        result = 31 * result + col;
        return result;
    }

    @Override
    public String toString() {
        return "Coordinate{" +
                "row=" + row +
                ", col=" + col +
                '}';
    }
}
